<div class="content-wrapper">
    <?php
    $form = $this->form;
    $form->prepare();

    ?>
    <?php echo $this->form()->openTag($form); ?>
    <section class="content-header">
        <?php echo $this->partial('layout/breadcrumb'); ?>

        <h1 class="pull-right" id="dbshop-menu-top">
            <a href="<?php echo $this->url('goods', [], ['query' => $this->queryData]);?>" class="btn btn-default btn-sm"><i class="fa fa-arrow-left"></i> <?php echo $this->translate('返回商品列表'); ?></a>
            <?php echo $this->HelpUrl('editVirtualGoods'); ?>
        </h1>
    </section>

    <section class="content">

        <div class="row">
            <div class="col-md-12">
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title"><?php echo $this->translate('虚拟商品'); ?></h3>
                    </div>

                    <div class="box-body">
                        <?php echo $this->formElement($form->get('virtualGoodsId')); ?>
                        <?php echo $this->formElementErrors($form->get('virtualGoodsId'), ['class'=>'error-message']); ?>
                        <div class="form-group">
                            <label class="col-sm-2 control-label"><?php echo $this->translate('补货虚拟商品名称'); ?></label>
                            <div class="col-sm-6">
                                <input type="text" readonly value="<?php echo $this->escapeHtml($this->goodsInfo->getGoodsName()); ?>" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label"><?php echo $this->translate('账号'); ?><span class="must_add_value">*</span></label>
                            <div class="col-sm-4">
                                <?php echo $this->formElement($form->get('virtualGoodsAccount')); ?>
                                <?php echo $this->formElementErrors($form->get('virtualGoodsAccount'), ['class'=>'error-message']); ?>
                            </div>
                            <div class="col-sm-2">
                                <?php echo $this->formElement($form->get('virtualGoodsAccountType')); ?>
                                <?php echo $this->formElementErrors($form->get('virtualGoodsAccountType'), ['class'=>'error-message']); ?>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label"><?php echo $this->translate('密码'); ?><span class="must_add_value">*</span></label>
                            <div class="col-sm-4">
                                <?php echo $this->formElement($form->get('virtualGoodsPassword')); ?>
                                <?php echo $this->formElementErrors($form->get('virtualGoodsPassword'), ['class'=>'error-message']); ?>
                            </div>
                            <div class="col-sm-2">
                                <?php echo $this->formElement($form->get('virtualGoodsPasswordType')); ?>
                                <?php echo $this->formElementErrors($form->get('virtualGoodsPasswordType'), ['class'=>'error-message']); ?>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label"><?php echo $this->translate('有效日期至'); ?></label>
                            <div class="col-sm-3">
                                <div class="input-group">
                                    <div class="input-group-addon">
                                        <i class="fa fa-calendar"></i>
                                    </div>
                                    <?php echo $this->formElement($form->get('virtualGoodsEndTime')); ?>
                                </div>
                                <?php echo $this->formElementErrors($form->get('virtualGoodsEndTime'), ['class'=>'error-message']); ?>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label"></label>
                            <div class="col-sm-4">
                                <button type="submit" class="btn btn-primary"><i class="fa fa-check"></i> <?php echo $this->translate('点击给虚拟商品补货'); ?></button>
                            </div>
                        </div>
                    </div>

                    <div class="box-body table-responsive no-padding">
                        <table class="table shop-table table-bordered">
                            <thead>
                            <tr class="shop-bg-thead">
                                <th width="15%"><?php echo $this->translate('账号'); ?></th>
                                <th width="6%"><?php echo $this->translate('账号类型'); ?></th>
                                <th width="15%"><?php echo $this->translate('密码'); ?></th>
                                <th width="8%"><?php echo $this->translate('密码类型'); ?></th>
                                <th width="6%"><?php echo $this->translate('状态'); ?></th>
                                <th width="8%"><?php echo $this->translate('到期日期'); ?></th>
                                <th width="15%"><?php echo $this->translate('订单编号'); ?></th>
                                <th width="10%"><?php echo $this->translate('操作'); ?></th>
                            </tr>
                            </thead>
                            <tbody id="virtual-goods-tbody">
                                <?php if($this->virtualGoodsList) foreach ($this->virtualGoodsList as $virtualValue) { ?>
                                    <tr id="virtual-goods-<?php echo $virtualValue->getVirtualGoodsId(); ?>">
                                        <td><?php echo $virtualValue->getVirtualGoodsAccount(); ?></td>
                                        <td><?php echo $this->goodsHelper()->getVirtualGoodsType($virtualValue->getVirtualGoodsAccountType()); ?></td>
                                        <td><?php echo $virtualValue->getVirtualGoodsPassword(); ?></td>
                                        <td><?php echo $this->goodsHelper()->getVirtualGoodsType($virtualValue->getVirtualGoodsPasswordType()); ?></td>
                                        <td><?php echo $this->goodsHelper()->getVirtualGoodsState($virtualValue->getVirtualGoodsState()); ?></td>
                                        <td><?php echo $virtualValue->getVirtualGoodsEndTime() > 0 ? '<span class="'.(($virtualValue->getVirtualGoodsState() != 2 && $virtualValue->getVirtualGoodsEndTime() < time()) ? 'text-red' : '').'">'.date("Y-m-d", $virtualValue->getVirtualGoodsEndTime()).'</span>' : $this->translate('无'); ?></td>
                                        <td><?php echo $virtualValue->getOrderSn(); ?></td>
                                        <td>
                                            <?php if ($virtualValue->getVirtualGoodsState() != 2) { ?>
                                            <a href="javascript:;" onclick="editVirtualGoods(<?php echo $virtualValue->getVirtualGoodsId(); ?>);"
                                               class="btn btn-xs btn-success"><i class="fa fa-pencil"></i> <?php echo $this->translate('编辑'); ?></a>
                                            <a href="javascript:;" onclick="deleteConfirm('<?php echo $this->translate('您确实要删除该虚拟商品账号信息吗?'); ?>', '<?php echo $this->url('goods', ['action'=>'deleteVirtualGoods', 'id'=>$virtualValue->getVirtualGoodsId()]); ?>');" class="btn btn-xs btn-danger"><i class="fa fa-trash"></i> <?php echo $this->translate('删除'); ?></a>
                                            <?php } ?>
                                        </td>
                                    </tr>
                                <?php } ?>
                            </tbody>
                        </table>
                    </div>

                </div>
            </div>
        </div>

    </section>
    <?php echo $this->form()->closeTag(); ?>

</div>
<script>
    laydate.render({
        elem: '#virtualGoodsEndTime'
    });

    $(document).ready(function() {
        $("#<?php echo $form->getName(); ?>").validate({
            rules: {
                virtualGoodsAccount: {
                    required: true
                },
                virtualGoodsPassword: {
                    required: true
                }
            },
            messages: {
                virtualGoodsAccount: {
                    required: "<?php echo $this->translate('账号不能为空!'); ?>"
                },
                virtualGoodsPassword: {
                    required: "<?php echo $this->translate('密码不能为空!'); ?>"
                }
            },
            submitHandler: function() {
                ajaxSubmit();
            }
        });
    });
    function ajaxSubmit() {
        $.post("<?php echo $this->url('goods', ['action'=> 'ajaxUpdateVirtualGoods', 'id' => $this->goodsInfo->getGoodsId()]);?>", $('#<?php echo $form->getName(); ?>').serialize(), function (data) {
            if(data.state == 'true') {
                if(data.type == 'add') {
                    $('#virtual-goods-tbody').prepend(data.html);
                } else {
                    $('#virtual-goods-'+data.virtualGoodsId).html(data.html);
                }
                $('#virtualGoodsId').val('');
                $('#<?php echo $form->getName(); ?>')[0].reset();
            } else shopMessage(data.messages);
        }, 'json');
    }

    //账号类型选择
    $('#virtualGoodsAccountType').change(function () {
        if(this.value == 2) {
            $('#virtualGoodsAccount').val('Auto Create');
            $('#virtualGoodsAccount').attr('readonly', true);
        } else {
            $('#virtualGoodsAccount').val('');
            $('#virtualGoodsAccount').attr('readonly', false);
        }
    });
    //密码类型选择
    $('#virtualGoodsPasswordType').change(function () {
        if(this.value == 2) {
            $('#virtualGoodsPassword').val('Auto Create');
            $('#virtualGoodsPassword').attr('readonly', true);
        } else {
            $('#virtualGoodsPassword').val('');
            $('#virtualGoodsPassword').attr('readonly', false);
        }
    });

    //编辑虚拟商品账号信息
    function editVirtualGoods(virtualGoodsId) {
        $.post("<?php echo $this->url('goods', ['action'=> 'ajaxInfoVirtualGoods', 'id' => $this->goodsInfo->getGoodsId()]);?>", {virtualGoodsId:virtualGoodsId}, function (data) {
            if(data.state == 'true') {
                $('#virtualGoodsId').val(data.virtualGoods.virtualGoodsId);
                $('#virtualGoodsAccount').val(data.virtualGoods.virtualGoodsAccount);
                $('#virtualGoodsPassword').val(data.virtualGoods.virtualGoodsPassword);
                $('#virtualGoodsEndTime').val(data.virtualGoods.virtualGoodsEndTime);
                if(data.virtualGoods.virtualGoodsAccountType == 2) {
                    $('#virtualGoodsAccount').val('Auto Create');
                    $('#virtualGoodsAccount').attr('readonly', true);
                } else $('#virtualGoodsAccount').attr('readonly', false);
                if(data.virtualGoods.virtualGoodsPasswordType == 2) {
                    $('#virtualGoodsPassword').val('Auto Create');
                    $('#virtualGoodsPassword').attr('readonly', true);
                } else $('#virtualGoodsPassword').attr('readonly', false);
                $("#virtualGoodsAccountType option[value='"+data.virtualGoods.virtualGoodsAccountType+"']").attr("selected", "selected");
                $("#virtualGoodsPasswordType option[value='"+data.virtualGoods.virtualGoodsPasswordType+"']").attr("selected", "selected");
            } else shopMessage(data.messages);
        }, 'json');
    }
</script>
